//
//  InputTextViewController.m
//  Translator
//
//  Created by crpatte2 on 10/10/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "InputTextViewController.h"


@implementation InputTextViewController
@synthesize inputBox, pickLanguage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSString *)translateToPigLatin:(NSString *)input {
    
    NSArray *words = [input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *translatedWords = [NSArray array];
    
    int n = [words count];
    NSString *word, *translatedWord, *prefix, *suffix, *firstChar, *lastChar;
    BOOL isConsonant, isCapitalized, hasPunctuation;
    
    
    for (int i = 0; i < n; i++) {
        isCapitalized = NO;
        hasPunctuation = NO;
        word = [words objectAtIndex:i];
        translatedWord = [word lowercaseString]; //translatedWord temporarily holds lowercase word
        
        if(![translatedWord isEqualToString:word]){ //if the two strings are different, one must have been capitalized
            isCapitalized = YES;
        }
        
        
        if([translatedWord length] > 1) {
      
            lastChar = [translatedWord substringFromIndex:[translatedWord length] - 1];
            hasPunctuation = ([lastChar isEqualToString:@","] || 
                              [lastChar isEqualToString:@"."] || 
                              [lastChar isEqualToString:@"?"] || 
                              [lastChar isEqualToString:@"!"]);
        
        
            if(hasPunctuation) {
                translatedWord = [translatedWord substringToIndex:[translatedWord length] - 1];
            } //chops off punctuation characters at end of word
        
            if(([translatedWord length] > 1) && [[translatedWord substringToIndex:2] isEqualToString:@"qu"]) { //check for a "qu"
                translatedWord = [[translatedWord substringFromIndex:2] stringByAppendingString:@"quay"];
            }
        
            else {
                prefix = translatedWord;
                suffix = @"";
                firstChar = [[prefix substringToIndex:1] lowercaseString];
                isConsonant = !([firstChar isEqualToString:@"a"] || 
                                [firstChar isEqualToString:@"e"] || 
                                [firstChar isEqualToString:@"i"] || 
                                [firstChar isEqualToString:@"o"] ||
                                [firstChar isEqualToString:@"a"]);
     
                while (isConsonant && ([prefix length] > 1)) {
                    suffix = [suffix stringByAppendingString:firstChar];
                    prefix = [prefix substringFromIndex:1];
     
                    firstChar = [[prefix substringToIndex:1] lowercaseString];
                    isConsonant = !([firstChar isEqualToString:@"a"] || 
                                    [firstChar isEqualToString:@"e"] || 
                                    [firstChar isEqualToString:@"i"] || 
                                    [firstChar isEqualToString:@"o"] ||
                                    [firstChar isEqualToString:@"u"]);   
                }
                translatedWord = [[prefix stringByAppendingString:suffix] stringByAppendingString:@"ay"];
            }
        }
        
        if(isCapitalized) {
            translatedWord = [translatedWord capitalizedString];
        } //restore proper capitalization
        
        if(hasPunctuation) {
            translatedWord = [translatedWord stringByAppendingString:[word substringFromIndex:[word length] - 1]];
        }
        
        translatedWords = [translatedWords arrayByAddingObject:translatedWord];
        
    }
    
    return [translatedWords componentsJoinedByString:@" "];
   
    
}


-(NSString *)translateToElmerFudd:(NSString *)input{
    NSArray *words = [input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSArray *translatedWords = [NSArray array];
    
    int wordCount = [words count];
    for (int i = 0; i < wordCount; i++){
        NSString *word = [words objectAtIndex:i];
        NSString *newWord = @"";
        for (int j = 0; j < [word length]; j++){
            NSString *achar = [NSString stringWithFormat:@"%c", [word characterAtIndex:j]];
            boolean_t changeMade = false;
            if ([achar isEqualToString:@"r"]){
                achar = @"w";
                changeMade = true;
            }
            else if ([achar isEqualToString:@"l"]){
                achar = @"w";
                changeMade = true;
            }
            if (j < [word length] - 1 && !changeMade){
                NSString *nchar = [NSString stringWithFormat:@"%c", [word characterAtIndex:j+1]];
                if ([achar isEqualToString:@"t"] && [nchar isEqualToString:@"h"]){
                    achar = @"f";
                    nchar = @"";
                    changeMade = true;
                }
                else if ([achar isEqualToString:@"u"] && [nchar isEqualToString:@"a"]){
                    achar = @"w";
                    changeMade = true;
                }
                newWord = [newWord stringByAppendingString:achar];
                if (changeMade) {
                    newWord = [newWord stringByAppendingString:nchar];
                    j++;
                }
            }
            else newWord = [newWord stringByAppendingString:achar];
        }
        translatedWords = [translatedWords arrayByAddingObject:newWord];
    }
    return [translatedWords componentsJoinedByString:@" "];
}

-(IBAction)textDoneEditing:(id)sender{
    [inputBox resignFirstResponder];
}

-(IBAction) testTranslatorMethod {
    Translated *translatedView = [[[Translated alloc] initWithNibName:@"Translated" bundle:nil] autorelease];
    
    NSString *inputText = inputBox.text;
    
    translatedView.originalText = inputText;
 
    if(pickLanguage.selectedSegmentIndex == 0){
        translatedView.translatedText = [self translateToPigLatin:inputText];
        translatedView.language = @"Pig Latin";
    }
    else {
        translatedView.translatedText = [self translateToElmerFudd:inputText];
        translatedView.language = @"Elmer Fudd";
    }
    
    
    [self.navigationController pushViewController:translatedView animated:YES];
}

@end
